//
//  logisticsVechileViewController.m
//  WHB_cargo
//
//  Created by 徐国庆 on 15/12/14.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "logisticsVechileViewController.h"
#import "ShipVechilesDetailViewController.h"

@interface logisticsVechileViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *array;

@end

@implementation logisticsVechileViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [[NSMutableArray alloc] init];
        // 循环创建三个bool类型开关 来控制当前section是否打开
        for (int i = 0; i < 3; i ++) {
            NSString *sectionSwitch = @"no";
            [self.array addObject:sectionSwitch];
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navtitle.text = @"物流商车辆列表";
    [self initWithView];
}

#pragma mark UITableViewDeleget
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *sectionSwitch = [self.array objectAtIndex:section];
    if ([sectionSwitch isEqualToString:@"yes"]) {
        return 7;
    } else{
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, 40)];
    
    // section上的按钮
    UIButton *logisticsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    logisticsButton.frame = CGRectMake(0, 0, kUIScreenWidth, 40);
    logisticsButton.tag = section + 1000;
    [logisticsButton setTitle:@"    测试物流" forState:UIControlStateNormal];
    logisticsButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [logisticsButton setTitleColor:[UIColor HexStrWithColor:@"3C3C3C"] forState:UIControlStateNormal];
    logisticsButton.backgroundColor = [UIColor HexStrWithColor:@"FCFCFC"];
    logisticsButton.layer.borderWidth = 0.5;
    logisticsButton.layer.borderColor = [UIColor HexStrWithColor:@"BEBEBE"].CGColor;
    [logisticsButton addTarget:self action:@selector(logisticsButtonAction:) forControlEvents:UIControlEventTouchDown];
    logisticsButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [bottomView addSubview:logisticsButton];
    
    return bottomView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifer = @"logisticsVechileCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
    }
    cell.textLabel.text = @"黑A88800";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ShipVechilesDetailViewController *shipVechilesDetailVC = [[ShipVechilesDetailViewController alloc] init];
    [self.navigationController pushViewController:shipVechilesDetailVC animated:YES];
}

#pragma mark 点击section按钮方法
- (void)logisticsButtonAction:(UIButton *)button{
    // 取出当前按钮的开关 看是开状态还是关状态
    NSString *tempSwitch = [self.array objectAtIndex:button.tag - 1000];
    if ([tempSwitch isEqualToString:@"no"]) {
        // 如果是关的状态就设置成开 并刷新列表
        tempSwitch = [NSString stringWithFormat:@"yes"];
        [self.array replaceObjectAtIndex:button.tag - 1000 withObject:tempSwitch];
        [self.tableView reloadData];
        return;
    } else if ([tempSwitch isEqualToString:@"yes"]){
        // 如果是开的状态就关掉 并刷新列表
        tempSwitch = @"no";
        [self.array replaceObjectAtIndex:button.tag - 1000 withObject:tempSwitch];
        [self.tableView reloadData];
    }
}

/**
 *  初始化列表
 */
- (void)initWithView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - 20) style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableHeaderView = [self headerView];
    [self.view addSubview:self.tableView];
}

/**
 *  列表头
 *
 *  @return 列表头
 */
- (UIView *)headerView{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, kNavHeight, kUIScreenWidth, 40)];
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth + 2, 40)];
    lineLabel.backgroundColor = [UIColor HexStrWithColor:@"FF8000"];
    lineLabel.text = @"审核状态:审核中，预计两个工作日完成";
    lineLabel.font = [UIFont systemFontOfSize:14];
    lineLabel.textColor = [UIColor whiteColor];
    lineLabel.textAlignment = 1;
    
    [view addSubview:lineLabel];
    
    return view;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
